<?php


namespace app\queue\stomp;


use common\service\EntityRelationshipService;
use Throwable;
use think\Exception;
use app\queue\BaseConsumer;
use common\service\EntityService;
use Webman\Stomp\RetryAbleConsumer;

/**
 * 实体发布时，异步更新任务上下游
 * Class TaskRelationUpdateByEntityPublishConsumer
 * @package app\queue\redis
 */
class TaskRelationUpdateByEntityPublishConsumer extends BaseConsumer implements RetryAbleConsumer
{
    // 要消费的队列名
    public $queue = 'task_relation_update_by_entity_publish';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * run
     * @param $data
     * @return void
     * @throws Throwable
     * @throws Exception
     */
    public function doConsume($data)
    {
        if (!empty($data['entity_id'] ?? '')) {
            EntityRelationshipService::getInstance()->syncTaskUpstreamByEntityId($data['entity_id']);
        }
    }

    /**
     * 重试间隔
     * @return int
     */
    public function getTtr()
    {
        return 10;
    }

    /**
     * 重试检查
     * @param $attempt
     * @param $error
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        trace(format_exception_info($error), 'ERR');
        return $attempt <= 3;
    }
}
